package com.api.oa.controller;


import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import com.api.oa.model.entity.YQNews;
import com.api.oa.services.YQNewsService;
import com.api.shopapi.controller.BaseController;
import com.api.shopapi.entity.MyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/yq/yQNews")
public class YQNewsController extends BaseController {

    @Autowired
    private YQNewsService iYQNewsService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody YQNews entity) {
        try {
            return iYQNewsService.add(entity) == 1 ? success(entity) : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/remove/{id}", method = RequestMethod.POST)
    public MyResult remove(@PathVariable Long id) {
        try {
            iYQNewsService.remove(id);
            return success("删除成功!");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public MyResult update(@RequestBody YQNews entity) {
        try {
            return iYQNewsService.update(entity) == 1 ? success("操作成功") : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public MyResult list(@RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "20") Integer pageSize, @RequestParam(required = false) String keyWord,
                         @RequestParam(required = false) String newType) {
        try {
            return success(iYQNewsService.list(keyWord,newType, pageIndex, pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "/one/{id}", method = RequestMethod.GET)
    public MyResult one(@PathVariable Long id) {
        try {
            return success(id == null ? null : iYQNewsService.one(id));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "/url/{id}", method = RequestMethod.GET)
    public void url(@PathVariable Long id, HttpServletResponse res) {
        try {
            YQNews one = iYQNewsService.one(id);

            if (one == null) { //404
                res.setCharacterEncoding("utf-8");
                res.setHeader("Content-type", "text/html;charset=UTF-8");
                TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
                Template template = engine.getTemplate("404.ftl");
                Map<String, String> map = new HashMap<>();
                template.render(map, res.getWriter());
            } else {
                if (one.getNewUrl().equals("")) {
                    res.setCharacterEncoding("utf-8");
                    res.setHeader("Content-type", "text/html;charset=UTF-8");
                    TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
                    Template template = engine.getTemplate("news.ftl");
                    Map<String, String> map = new HashMap<>();
                    map.put("title",one.getNewTitle());
                    map.put("content",one.getNewContent());
                    template.render(map, res.getWriter());
                } else {
                    res.sendRedirect(one.getNewUrl());
                }
            }

        } catch (Exception e) {
           e.printStackTrace();
        }
    }


}
